package com.hqyj.blog.server.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.blog.server.entity.Users;
import com.hqyj.blog.server.mapper.UsersMapper;
import com.hqyj.blog.server.service.UsersService;
import com.hqyj.blog.server.util.MyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.transaction.Transactional;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Override
    public Users login(String identify, String password) throws Exception {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        //判断一下你传进来的identify是手机还是邮箱；
        if (MyUtils.isMobileNumber(identify)) {
            queryWrapper.eq("mobile", identify);
        } else if (MyUtils.isEmail(identify)) {
            queryWrapper.eq("email", identify);
        } else {
            queryWrapper.eq("username", identify);
        }

        Users loginSuccessUser = super.getOne(queryWrapper);
        if (loginSuccessUser != null) {

            if (MyUtils.checkPassword(password, loginSuccessUser.getPassword())) {
                return loginSuccessUser;
            }
            return null;
        }
        return null;
    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public boolean reg(Users u) throws Exception {
        try {
            //随机生成一个用户名
            u.setUsername(IdUtil.simpleUUID()); //保存随机生成的用户名
            u.setPassword(MyUtils.encodePassword(u.getPassword()));
            u.setGender("男");
            u.setBirthday("2000-01-01");
            //设置默认的头像
            u.setAvatar("https://img.simoniu.com/vue-color-avatar11.png");
            return super.save(u);

        } catch (Exception ex) {
            ex.printStackTrace();
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    @Override
    public boolean checkIdentifyIsReg(String identify) throws Exception {

        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        queryWrapper.eq("mobile", identify).or().eq("email", identify);
        return super.list(queryWrapper).size() > 0;

    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public boolean updateUsersProfile(Users u) throws Exception {
        try {
            return super.updateById(u);
        }catch (Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public Users queryUserProfileByUid(Integer uid) throws Exception {
        return super.getById(uid);
    }
}
